package com.oa.srms.apps.dao.limit.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.srms.apps.dao.limit.ILimitInfoDao;
import com.oa.srms.apps.entity.limit.LimitInfoValue;
import com.oa.srms.apps.entity.limit.PostLimitValue;

public class LimitInfoDaoImpl extends HibernateDaoSupport implements
		ILimitInfoDao {
	@Autowired
	private List<LimitInfoValue> list;

	// DAO method
	
	@Override
	public int addLimitInfo(LimitInfoValue limitInfo) throws Exception {
		if (limitInfo != null) {
			this.getHibernateTemplate().save(limitInfo);
		}
		return limitInfo.getLimitId();
	}

	@SuppressWarnings("unchecked")
	@Override
	public int countTPages(int rows) throws Exception {
		int pageNumbers = 0;
		String hql = "FROM LimitInfoValue liv";
		list = this.getHibernateTemplate().find(hql);
		int numbers = list.size();
		if (numbers <= rows) {
			pageNumbers = 1;
		} else {
			if (numbers % rows == 0) {
				pageNumbers = numbers / rows;
			} else {
				pageNumbers = numbers / rows + 1;
			}
		}
		return pageNumbers;
	}

	@Override
	public boolean editLimitInfo(LimitInfoValue limitInfo) throws Exception {
		if (limitInfo != null){
			try {
				this.getHibernateTemplate().update(limitInfo);
			} catch (Exception e) {
				return false;
			}
			return true;
		}
		return false;
	}

	@Override
	public LimitInfoValue queryByLimitId(LimitInfoValue limitInfo)
			throws Exception {
		if (limitInfo != null) {
			return (LimitInfoValue) this.getHibernateTemplate().get(LimitInfoValue.class, limitInfo.getLimitId());
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<LimitInfoValue> queryByParentId(LimitInfoValue limitInfo, PostLimitValue postLimit)
			throws Exception {
		String limitIds = postLimit.getLimitIds();
		if (limitInfo != null) {
			int parentId = limitInfo.getParentId();
			if (parentId == 0) {
				return this.getHibernateTemplate().find("FROM LimitInfoValue liv WHERE liv.states=1 AND liv.parentId=? AND liv.limitId in ("+ limitIds +") AND (SELECT count(lifv.limitId) FROM LimitInfoValue lifv WHERE lifv.parentId = liv.limitId) > 0", parentId);
			}
			return this.getHibernateTemplate().find("FROM LimitInfoValue liv WHERE liv.parentId = ? AND liv.states=1 AND liv.limitId in ("+ limitIds +")", parentId);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<LimitInfoValue> queryLimitInfoWithPage(final int rows, final int pageNo)
			throws Exception {
		final String hql = "FROM LimitInfoValue liv";
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				query.setFirstResult(rows * (pageNo - 1));
				query.setMaxResults(rows);
				list = query.list();
				return list;
			}
		});
		return list;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<LimitInfoValue> queryByLimitIds(String limitIds)
			throws Exception {
		String hql = " FROM LimitInfoValue liv WHERE liv.limitId in ("+limitIds+") AND (SELECT count(liv1.limitId) FROM LimitInfoValue liv1 WHERE liv1.parentId = liv.limitId) = 0 AND liv.parentId != 0";
		return this.getHibernateTemplate().find(hql);
	}

}
